package com.shizhanzhe.zxbapp.vip;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.shizhanzhe.zxbapp.R;
import com.shizhanzhe.zxbapp.bean.GameList;
import com.shizhanzhe.zxbapp.bean.UserBean;
import com.shizhanzhe.zxbapp.bean.XW;
import com.shizhanzhe.zxbapp.utils.SharedPreferencesUtils;
import com.shizhanzhe.zxbapp.utils.Util;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zz9527 on 2018/12/5.
 */
public class XWAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private List<XW.ItemsBean> list;
    private Context context;

    public XWAdapter(Context context, List<XW.ItemsBean> list) {
        UserBean.divide = (String) SharedPreferencesUtils.getParam(context, "divide", "");
        UserBean.vipdivide = (String) SharedPreferencesUtils.getParam(context, "vipdivide", "");
        UserBean.uid = (int) SharedPreferencesUtils.getParam(context, "uid", 0);
        this.list = list;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final ViewHold hold;
        if (view == null) {
            view = inflater.inflate(R.layout.adapter_game, null);
            hold = new ViewHold();
            hold.title = view.findViewById(R.id.game_title);
            hold.iv = view.findViewById(R.id.game_iv);
            hold.fl = view.findViewById(R.id.flowlayout);
            hold.price = view.findViewById(R.id.game_price);
            view.setTag(hold);
        } else {
            hold = (ViewHold) view.getTag();
        }
        XW.ItemsBean bean = list.get(i);
        hold.title.setText(bean.getAdname());
        String showmoney = bean.getShowmoney();
        showmoney=showmoney.replace("元","");
        if (!"".equals(showmoney)){
            if (UserBean.level > 0) {
                hold.price.setText("+" + Util.Sub(Double.parseDouble(showmoney) * 2 * (Double.parseDouble(UserBean.vipdivide) + Double.parseDouble(UserBean.divide)) + "") + "元");
            } else {
                hold.price.setText("+" + Util.Sub(Double.parseDouble(showmoney) * 2 * Double.parseDouble(UserBean.divide) + "") + "元");
            }
        }else {
            hold.price.setText("暂无数据");
        }

        Glide.with(context).load(bean.getImgurl()).into(hold.iv);
        ArrayList<String> strlist = new ArrayList<>();
        strlist.add(bean.getIntro());
        hold.fl.setAdapter(new TagAdapter<String>(strlist) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView tv = (TextView) inflater.inflate(R.layout.tv,
                        hold.fl, false);
                tv.setText(s);
                return tv;
            }
        });
        return view;
    }

    class ViewHold {
        TextView title;
        ImageView iv;
        TagFlowLayout fl;
        TextView price;
    }
}